import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import threads.BuscaNFBlockingQueue;
import threads.BuscaNFLock;
import threads.BuscaNFWaitNotify;
import threads.ExibeComissao;
import threads.ProcessaNFBlockingQueue;
import threads.ProcessaNFLock;
import threads.ProcessaNFWaitNotify;
import entidades.Nota;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import javax.swing.JFrame;

public class TelaFiltrosRecalculo extends JFrame {

	public TelaFiltrosRecalculo() throws ParseException {
		setSize(300, 200);
		setLocationRelativeTo(null);
		setTitle("Recálculo de Comissão");
		setResizable(false);
		// setModal(true);
		initComponents();
		setVisible(true);
		// setDefaultCloseOperation(EXIT_ON_CLOSE);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

	}

	private void initComponents() throws ParseException {

		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(4, 2));

		panel.add(new JLabel("NFs por Lote:", SwingConstants.CENTER));
		final JTextField campoLote = new JFormattedTextField(new MaskFormatter(
				"***"));
		campoLote.setText("20");
		panel.add(campoLote);

		panel.add(new JLabel("Vendedor:", SwingConstants.CENTER));
		final JTextField campoVend = new JFormattedTextField(new MaskFormatter(
				"**"));
		panel.add(campoVend);

		panel.add(new JLabel("Nota Fiscal Nro:", SwingConstants.CENTER));
		final JTextField campoNota = new JFormattedTextField(new MaskFormatter(
				"*******"));
		panel.add(campoNota);

		JPanel panel2 = new JPanel();
		panel2.setLayout(new GridLayout(2, 2));
		panel2.add(new JLabel("Data Inicial:", SwingConstants.CENTER));
		JTextField campoData1 = new JFormattedTextField(new MaskFormatter(
				"##/##/####"));
		campoData1.setText("01012009");
		panel2.add(campoData1);

		panel2.add(new JLabel("Data Final:", SwingConstants.CENTER));
		JTextField campoData2 = new JTextField();
		campoData2 = new JFormattedTextField(new MaskFormatter("##/##/####"));
		campoData2.setText("30122010");
		panel2.add(campoData2);

		getContentPane().add(panel, BorderLayout.NORTH);
		getContentPane().add(panel2, BorderLayout.CENTER);

		JPanel panelBotoes = new JPanel();
		JButton recalc = new JButton("Recalcular");
		recalc.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int lote = 20, vend = 0, nf = 0;
				try {
					lote = Integer.parseInt(campoLote.getText());
					vend = Integer.parseInt(campoVend.getText());
					nf = Integer.parseInt(campoNota.getText());
				} catch (Exception ad) {
				}
				recalcular(lote, vend, nf);
				JOptionPane.showMessageDialog(null, "Recalculando!!!");
				setVisible(false);

			}
		});

		panelBotoes.add(recalc);

		JButton btCancelar = new JButton("Cancelar");
		panelBotoes.add(btCancelar);
		btCancelar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});

		getContentPane().add(panelBotoes, BorderLayout.SOUTH);
	}

	public static void main(String[] args) throws ParseException,
			InterruptedException {
		new TelaFiltrosRecalculo();
	}

	public void recalcular(int lote, int vencod, int nf) {
		Nota nota = new Nota();
		nota.setLote(lote);

		BlockingQueue<Nota> listaBlocking = new ArrayBlockingQueue<Nota>(lote);// plus
		nota.setListaBlockingQueue(listaBlocking);
		for (int i = 0; i < 1; i++) {
			ProcessaNFBlockingQueue procQueue = new ProcessaNFBlockingQueue(
					nota, i);
			procQueue.start();
		}

		BuscaNFBlockingQueue buscaQueue = new BuscaNFBlockingQueue(nota, vencod);
		buscaQueue.start();
		for (int i = 0; i < 1; i++) {
			ProcessaNFLock procLock = new ProcessaNFLock(nota, i);
			procLock.start();
		}
		BuscaNFLock buscaLock = new BuscaNFLock(nota, vencod);
		buscaLock.start();

		// nota.setInicio(0);
		for (int i = 0; i < 1; i++) {
			ProcessaNFWaitNotify procWait = new ProcessaNFWaitNotify(nota,
					i + 1);
			procWait.start();
		}
		BuscaNFWaitNotify calcWait = new BuscaNFWaitNotify(nota, vencod);
		calcWait.start();

		ExibeComissao exibe = new ExibeComissao(nota, vencod);
		exibe.start();
	}
}
